// web/scripts/employee/mark.ts
const bcrypt = require("bcryptjs")

// @ts-ignore
const {PrismaClient} = require('@prisma/client');
// @ts-ignore
const db = new PrismaClient()

// @ts-ignore
const {faker} = require('@faker-js/faker/locale/zh_CN');

// @ts-ignore
let count = 20

function createRandomUser() {
    return {
        id: faker.string.nanoid(),
        name: faker.person.fullName(),
        username: faker.internet.username(), // before version 9.1.0, use userName()
        image: faker.image.url(),
        password: '$2a$10$E.YZoVDrqold5Oy5cVXydegE8e6Edir1.6PGHHxuEHwFiG0XaX21K',
        role: 'admin',
        isTwoFactorEnable: false,
        email: faker.internet.email(),
        // emailVerified: true,
        emailVerified: faker.date.past(),
    };
}

const users = faker.helpers.multiple(createRandomUser, {
    count,
});

function createRandomCompany() {
    return {
        id: faker.string.nanoid(),
        image: faker.image.url(),
        description: faker.hacker.phrase(),
        name: faker.company.name(), // before version 9.1.0, use userName()
        // location: `${faker.location.country()} - ${faker.location.city()} - ${faker.location.street()}`,
        location: `${faker.location.state()}, ${faker.location.city()}, ${faker.location.streetAddress()}`,
        creationTime: faker.date.past()
    };
}

const companies = faker.helpers.multiple(createRandomCompany, {
    count,
});

async function createRandomHr() {
    for (const company of companies) {
        await db.hr.create({
            data: {
                companyId: company.id,
                image: faker.image.url(),
                // username: faker.internet.username(),
                username: faker.person.fullName(),
                userId: users[Math.floor(Math.random() * 10)].id
            }
        })
    }
}

function createRandomCity() {
    return {
        id: faker.string.nanoid(),
        name: faker.location.city()
    }
}

const cities = faker.helpers.multiple(createRandomCity, {
    count,
});

const educations = [
    {
        id: faker.string.nanoid(),
        name: '博士'
    },
    {
        id: faker.string.nanoid(),
        name: '研究生'
    },
    {
        id: faker.string.nanoid(),
        name: '本科'
    },
    {
        id: faker.string.nanoid(),
        name: '大专及以下'
    },
]

async function createRandomEmployee() {
    const hrs = await db.hr.findMany()
    for (let i = 0; i < count; i++) {
        await db.employee.create({
            data: {
                companyId: companies[Math.floor(Math.random() * 10)].id,
                // name: `${faker.commerce.department()}  ${faker.commerce.productDescription()}`,
                name: `${faker.company.name()} - ${faker.commerce.department()}`,
                salary: parseInt(faker.commerce.price()),
                description: faker.hacker.phrase(),
                cityId: cities[Math.floor(Math.random() * 10)].id,
                educationId: educations[Math.floor(Math.random() * 4)].id,
                hrId: hrs[Math.floor(Math.random() * hrs.length)].id,
            }
        })
    }
}

function createRandomStudent() {
    return ({
        name: faker.person.fullName(),
        educationId: educations[Math.floor(Math.random() * 4)].id,
        school: `${faker.location.state()}, ${faker.location.city()}, ${faker.location.streetAddress()} 学校`,
        // school: `${faker.location.streetAddress()}, ${faker.location.city()}, ${faker.location.state()} 学校`,
        userId: users[Math.floor(Math.random() * 10)].id
    })
}

const students = faker.helpers.multiple(createRandomStudent, {
    count,
});

// @ts-ignore
async function main() {
    // 创建虚拟用户数据
    await db.user.createMany({
        data: [...users]
    })

    // 创建虚拟公司数据
    const companiesDb = await db.company.createMany({
        data: [
            ...companies
        ]
    })

    // 创建虚拟公司hr数据
    await createRandomHr()

    // 创建虚拟城市数据
    await db.city.createMany({
        data: [
            ...cities
        ]
    })

    // 学历级别数据
    await db.education.deleteMany({}) // 删除防止重复创建
    await db.education.createMany({
        data: [...educations]
    })

    // 虚拟职位数据
    await createRandomEmployee()

    // 虚拟学生数据
    await db.student.createMany({
        data: [...students]
    })

    console.log(`ok`)
}

main()
